
package com.areamodule.dao;
import com.areamodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbAreaDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbArea tbArea=null;
    private List<TbArea> tbAreaList=null;
    private Map<Integer,TbArea> areaMap=null;
    public boolean add(TbArea inTbArea){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Area(AreaPostCode,AreaName,ParentAreaId,AreaMemo,CreateTime) values(?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbArea.getAreaPostCode());
        pt.setString(2,inTbArea.getAreaName());
        if(inTbArea.getParentAreaId()==0)pt.setNull(3, Types.INTEGER);
        else
        pt.setInt(3,inTbArea.getParentAreaId());
        pt.setString(4,inTbArea.getAreaMemo());
        pt.setString(5,inTbArea.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public boolean del(int inAreaId){
        return UtilDAO.del("Tb_Area", "AreaId", inAreaId);
    }
    public boolean edit(TbArea inTbArea){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Area set AreaPostCode=?,AreaName=?,ParentAreaId=?,AreaMemo=? where AreaId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbArea.getAreaPostCode());
        pt.setString(2,inTbArea.getAreaName());
        pt.setInt(3,inTbArea.getParentAreaId());
        pt.setString(4,inTbArea.getAreaMemo());
        pt.setInt(5,inTbArea.getAreaId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public TbArea getTbAreaByAreaId(int inAreaId) {
        tbArea=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Area where AreaId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inAreaId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbArea == null)tbArea = getTbArea(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbArea;
    }
    public List<TbArea> getTbAreaAll() {
        tbAreaList=new ArrayList<TbArea>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Area";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbAreaList.add(getTbArea(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
        	ConnectDB.closeSqlConnection();
            
        }
        return tbAreaList;
    }
    /**
     * 
     *@author qingyu zhang
     *@function:
     * @param areaId
     * @return
     *2011-5-19
     *上午08:06:33
     *ProjectManager
     *com.areamodule.dao
     *List<TbArea>
     */
    public List<TbArea> getTbAreaAll(String areaId) {
    	tbAreaList=new ArrayList<TbArea>();
    	try{
    		conn = ConnectDB.getSqlServerConnection();
    		sql = "select * from Tb_Area where AreaId in("+areaId+")";
    		pt = conn.prepareStatement(sql);
    		rs = pt.executeQuery();
    		while (rs.next()) {
    			tbAreaList.add(getTbArea(rs));
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	}finally {
    		ConnectDB.closeSqlConnection();
    		
    	}
    	return tbAreaList;
    }
    public Map<Integer,TbArea> getTbArea(){
    	areaMap = new HashMap<Integer,TbArea>();
    	try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Area";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
            	areaMap.put(getTbArea(rs).getAreaId(), getTbArea(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return areaMap;
    }
    public List<TbArea> getTbAreaAll(VskPage vp) {
        tbAreaList=new ArrayList<TbArea>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Area";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Area", "AreaId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbAreaList.add(getTbArea(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbAreaList;
    }
    public TbArea getTbArea(ResultSet rs) throws Exception{
        TbArea tbArea = new TbArea();
        tbArea.setAreaId(rs.getInt("AreaId"));
        tbArea.setAreaPostCode(rs.getString("AreaPostCode"));
        tbArea.setAreaName(rs.getString("AreaName"));
        tbArea.setParentAreaId(rs.getInt("ParentAreaId"));
        tbArea.setAreaMemo(rs.getString("AreaMemo"));
        tbArea.setCreateTime(rs.getString("CreateTime"));
        return tbArea;
    }
}
